//
//  Update.m
//  Youhuiju
//
//  Created by 余尚飞 on 16/9/19.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "UpdateView.h"

@interface UpdateView ()<UITextViewDelegate>
@property (strong, nonatomic) UITextView *updateTextView;//更新内容
@property (strong, nonatomic) UILabel    *versionLabel;//版本号
@property (strong, nonatomic) UILabel    *characteristicsLabel;//新版本特性
@property (nonatomic,strong)  UIImageView *bgView;//背景
@end

@implementation UpdateView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initViews];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initViews];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initViews];
    }
    return self;
}

- (void)initViews{
    
    self.image = LoadImage(@"newVersion");
    self.userInteractionEnabled = YES;
    
    UIButton *updateButton = [UIButton buttonWithType:UIButtonTypeCustom];//升级
//    updateButton.backgroundColor = [UIColor greenColor];
    
    UIButton *noUpdateButton = [UIButton buttonWithType:UIButtonTypeSystem];//取消升级
//    noUpdateButton.backgroundColor = [UIColor yellowColor];
    [noUpdateButton setBackgroundImage:LoadImage(@"noUpdate") forState:UIControlStateNormal];
    
    [updateButton addTarget:self action:@selector(update:) forControlEvents:UIControlEventTouchUpInside];
    [noUpdateButton addTarget:self action:@selector(noUpdate:) forControlEvents:UIControlEventTouchUpInside];
    
    self.updateTextView = [[UITextView alloc]initWithFrame:CGRectZero];
//    self.updateTextView.backgroundColor = [UIColor redColor];
    //    self.updateTextView.userInteractionEnabled = NO;
    self.updateTextView.text = @"1.提升了安全性\n2.优化了收益详情页面\n3.增加了声音开关按钮\n4.更换银行卡和登录手机号的流程优化";
    self.updateTextView.delegate = self;
    self.updateTextView.showsVerticalScrollIndicator = NO;
        self.updateTextView.textColor = [UIColor colorWithHexString:@"#666666"];
        self.updateTextView.font = [UIFont systemFontOfSize:15 * FitH];
    //self.updateTextView.text = @"1.szdjfksgjnsdlfsnfdfg\n2.sdhfbsdfkjngdfg\n3.sdnrgfdfvngtn;lsfdghm\n4.sfdkujhflhmghj上刊登";
    
    //新版本号
    self.versionLabel = [[UILabel alloc]initWithFrame:CGRectZero];
//    self.versionLabel.backgroundColor = [UIColor blackColor];
    self.versionLabel.textColor = [UIColor colorWithHexString:@"#333333"];
    self.versionLabel.font = [UIFont systemFontOfSize:11 * FitH];
    self.versionLabel.text = @"V1.1版本";
    self.versionLabel.textAlignment = NSTextAlignmentCenter;
    
    //新版本特性
    self.characteristicsLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    self.characteristicsLabel.textColor = [UIColor colorWithHexString:@"#333333"];
    self.characteristicsLabel.font = [UIFont systemFontOfSize:15 * FitH];
    self.characteristicsLabel.text = @"版本新特性";
    self.characteristicsLabel.textAlignment = NSTextAlignmentCenter;
    
    
    [self addSubview:updateButton];
    [self addSubview:noUpdateButton];
    [self addSubview:self.updateTextView];
    [self addSubview:self.versionLabel];
    [self addSubview:self.characteristicsLabel];
    
    //masonry  布局
    //版本更新按钮
    [updateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(164*FitW));
        make.height.equalTo(@(45*FitH));
//        make.right.equalTo(self.mas_centerX).offset(- 10*FitW);
        make.centerX.equalTo(self.mas_centerX);
        make.bottom.equalTo(self.mas_bottom).offset(- 35*FitH);
    }];
    
    //更新内容介绍
    [self.updateTextView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.mas_top).offset(155 *FitH);
        make.height.equalTo(@(105 * FitH));
        make.left.equalTo(self.mas_left).offset(41 * FitW);
        make.right.equalTo(self.mas_right).offset(-41 * FitW);
        make.bottom.equalTo(updateButton.mas_top).offset(- 5 * FitH);
    }];
    
    //新版本特性
    [self.characteristicsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.top.equalTo(self.mas_top).offset(155 *FitH);
//        make.height.equalTo(@(95 * FitH));
        make.left.equalTo(self.updateTextView.mas_left);
//        make.right.equalTo(self.mas_right).offset(-41 * FitW);
        make.bottom.equalTo(self.updateTextView.mas_top).offset(- 5 * FitH);
    }];
    
    //版本号
    [self.versionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.equalTo(@(57 * FitW));
        make.height.equalTo(@(12*FitH));
        make.right.equalTo(self.updateTextView.mas_right);
        make.bottom.equalTo(self.characteristicsLabel.mas_top).offset(-0 * FitH);
    }];

    //取消更新按钮
    [noUpdateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(25 * FitW));
        make.height.equalTo(@(25 * FitH));
        make.right.equalTo(self.mas_right);
        make.bottom.equalTo(self.mas_bottom).offset(-329 * FitH);
    }];
    
    
}

- (void)setVersion:(NSString *)version
{
//    self.versionLabel.text  = [NSString stringWithFormat:@"V%@",version];
    self.versionLabel.text = @"V1.1版本";
}

- (void)setUpdateNote:(NSString *)updateNote
{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 4;// 字体的行间距
    
//    NSString *myTest = @"1.提升了安全性\n2.优化了收益详情页面\n3.增加了声音开关按钮\n4.更换银行卡和登录手机号的流程优化";
    NSString *myTest = @"fjs;kjgsjd'fg'sakjdgfksd";
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:15 * FitH],
                                 NSParagraphStyleAttributeName:paragraphStyle,
                                 NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#666666"]};
    self.updateTextView.attributedText = [[NSAttributedString alloc] initWithString:myTest attributes:attributes];
}

- (void)show
{
    self.alpha = 1;
    self.bgView.alpha = 1;
    [[UIApplication sharedApplication].keyWindow addSubview:self.bgView];//设置透明背景
    
    self.center = [UIApplication sharedApplication].keyWindow.center;
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

- (void)update:(UIButton *)sender
{
    NSURL *URL = [NSURL URLWithString:self.urlString];
    
    if ([[UIApplication sharedApplication] canOpenURL:URL]) {
        [[UIApplication sharedApplication] openURL:URL];
    }
}

- (void)noUpdate:(UIButton *)sender
{
    
    [UIView animateWithDuration:.7 animations:^{
        self.alpha = 0.0;
        self.bgView.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.bgView removeFromSuperview];
    }];
    
}

- (UIImageView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        UIColor *gray = [UIColor blackColor];
        _bgView.backgroundColor = [gray colorWithAlphaComponent:0.5];
        _bgView.userInteractionEnabled = YES;
    }
    return _bgView;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    return  NO;
}

@end

